package com.yami.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.dto.BindUserInfoDto;
import com.yami.shop.distribution.common.service.DistributionBindUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yami
 */
@RestController
@RequestMapping("/p/distribution")
@Tag(name = "分销员绑定用户")
@Slf4j
public class DistributionBindUserController {

    @Autowired
    private DistributionBindUserService distributionBindUserService;

    @PostMapping("/bindUser")
    @Operation(summary =  "绑定用户", description = "根据分销员卡号绑定用户")
    @Parameters({
            @Parameter(name = "cardNo", description = "卡号", required = true)
    })
    public ServerResponseEntity<Void> bindUser(@RequestBody String cardNo) {
        // 没有扫码绑定功能，先注释掉
//        String userId = SecurityUtils.getUser().getUserId();
//
//        log.info("绑定用户cardNo:{}",cardNo);
//
//        if (StrUtil.isBlank(cardNo)) {
//            log.error("获取推广员信息失败");
//            return ServerResponseEntity.success();
//        }
//
//        DistributionUser sharerUser = distributionUserService.getByCardNo(cardNo);
//
//        if (sharerUser == null) {
//            log.error("获取推广员信息失败");
//            return ServerResponseEntity.success();
//        }
//        if (sharerUser.getState() != 1) {
//            log.error("推广员状态异常");
//            return ServerResponseEntity.success();
//        }
//        Long shopId = sharerUser.getShopId();
//
//        DistributionRecruitConfigVO distributionConfigVO = sysConfigService.getSysConfigObject(Constant.DISTRIBUTION_RECRUIT_CONFIG, DistributionRecruitConfigVO.class);
//        if (distributionConfigVO.getState() == 0) {
//            log.error("推广计划已关闭");
//            return ServerResponseEntity.success();
//        }
//
//
//        ServerResponse<DistributionUser> serverResponse = distributionBindUserService.bindDistribution(sharerUser, userId, 0);
//        if (!serverResponse.isSuccess()) {
//            log.error(serverResponse.getMsg());
//        }
        return ServerResponseEntity.success();
    }


    @GetMapping("/bindUserList")
    @Operation(summary =  "绑定用户列表", description = "获取分销员所绑定的用户列表")
    public ServerResponseEntity<IPage<BindUserInfoDto>> bindUserList(PageParam pageParam) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        String userId = SecurityUtils.getUser().getUserId();
        return ServerResponseEntity.success(distributionBindUserService.bindUserList(pageParam,shopId,userId));
    }
}
